package com.abe.utils;

import java.io.File;

import com.abe.app.ManagerAPP;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;

/**
 * 跟App相关的辅助类
 * 
 * 
 * 
 */
public class AppUtils {

	public AppUtils() {

	}

	/**
	 * 获取应用程序名称
	 */
	public static String getAppName(Context context) {
		try {
			PackageManager packageManager = context.getPackageManager();
			PackageInfo packageInfo = packageManager.getPackageInfo(
					context.getPackageName(), 0);
			int labelRes = packageInfo.applicationInfo.labelRes;
			return context.getResources().getString(labelRes);
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * [获取应用程序版本名称信息]
	 * 
	 * @param context
	 * @return 当前应用的版本名称
	 */
	public static String getVersionName(Context context) {
		try {
			PackageManager packageManager = context.getPackageManager();
			PackageInfo packageInfo = packageManager.getPackageInfo(
					context.getPackageName(), 0);
			return packageInfo.versionName;

		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * [是否需要更新]
	 * 
	 * @param NewestVersionName
	 * */
	public static boolean isNeedUpdate(String NewestVersionName) {
		String[] NewestVersionInfo = NewestVersionName.split(".");
		String[] VersionInfo = AppUtils.getVersionName(
				ManagerAPP.getContext()).split(".");
		int len = NewestVersionInfo.length > VersionInfo.length ? VersionInfo.length
				: NewestVersionInfo.length;
		for (int i = 0; i < len; i++) {
			int nv = Integer.parseInt(NewestVersionInfo[i]);
			int v = Integer.parseInt(VersionInfo[i]);
			if (nv > v) {
				return true;
			}
		}
		if (NewestVersionInfo.length > VersionInfo.length) {
			return true;
		}
		return false;
	}
	
	/**
	 * @see 安装apk
	 */
	public static void installNewApk(Context context, String filePath) {
		Intent intent = new Intent(Intent.ACTION_VIEW);
		intent.setDataAndType(Uri.fromFile(new File(filePath)),
				"application/vnd.android.package-archive");
		context.startActivity(intent);
	}

}
